package com.ygqh.baby.observer;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgOrderPayment;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.*;
import com.ygqh.baby.utils.DateConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 * @author admin
 */
@Component
public class PaySuccessMsgObserver implements Observer {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxSmallMessageService smallMessageService;
    @Autowired
    private YgOrderPaymentService ygOrderPaymentService;
    @Autowired
    private YgUserService ygUserService;
    @Autowired
    private YgOrderService ygOrderService;
    @Autowired
    private RedisDao redisDao;

    @Override
    public void update(Observable o, Object arg) {
        logger.info("PaySuccessMsgObserver.update: " + arg);
        OrderPayModel model = arg != null ? (OrderPayModel) arg : null;
        if (model == null || model.getOrderId() == null) {
            logger.error(o.getClass().getName() + ": orderCode 为空");
            return;
        }

        try {
            YgOrderPayment orderPayment = ygOrderPaymentService.findByPayCode(model.getPaymentCode());
            if (Constant.YIJIADIAN_PLATFORM_NO.equals(orderPayment.getPlatformNo())) {
                // 壹家店
                YgOrder order = ygOrderService.findOrderDetailByOrderId(model.getOrderId());
                smallMessageService.sendOrderPaySuccessMsgAsync(order, orderPayment.getPrepayId());
            } else  if (AppType.Small.equals(orderPayment.getAppType())) {
                // 壹果的店
                YgOrder order = ygOrderService.findByOrderId(model.getOrderId());
                Boolean canSend = canSend(order.getUserId());
                System.out.println(order.getOrderId() + " ============= " + canSend);
                if (canSend) {
                    YgUser user = ygUserService.findById(order.getUserId());
                    smallMessageService.sendQunMsg(orderPayment.getPrepayId(), null, null, user.getSmallOpenId());
                    setToRedis(order.getUserId(), order.getOrderId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送消息失败", e);
        }

    }

    private final String REDIS_KEY = "Small_Qun_Msg_Key_%s";

    private Boolean canSend(Long userId) {
        if ("102775".equals(userId.toString())) {
            return true;
        }
        String res = redisDao.get(String.format(REDIS_KEY, userId.toString()));
        return StringUtil.isBlank(res);
    }

    private void setToRedis(Long userId, Long orderId) {
        Date date = DateConvertUtils.addDay(new Date(), 30);
        redisDao.set(String.format(REDIS_KEY, userId.toString()), orderId.toString(), date.getTime());
    }


}
